//
//  MsgSysVC.m
//  iphoneLive
//
//  Created by QuanQu on 2018/8/2.
//  Copyright © 2018年 cat. All rights reserved.
//

#import "MsgSysVC.h"
#import "MsgSysModel.h"
#import "MsgSysCell.h"
#import "PubH5.h"

@interface MsgSysVC ()<UITableViewDelegate,UITableViewDataSource>
{
    int _paging;
}
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSArray *sysModels;

@end

@implementation MsgSysVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self pullData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Black_Cor;
    self.dataArray = [NSMutableArray array];
    self.sysModels = [NSArray array];
    _paging = 1;
    [self creatNavi];
    [self.view addSubview:self.tableView];
}

#pragma mark -
- (NSArray *)sysModels {
    
    NSMutableArray *m_array = [NSMutableArray array];
    for (NSDictionary *dic in _dataArray) {
        MsgSysModel *model = [MsgSysModel modelWithDic:dic lisModel:_listModel];
        [m_array addObject:model];
    }
    _sysModels = m_array;
    return _sysModels;
}

-(void)pullData {
    NSString *url;
    if ([_listModel.uidStr isEqual:@"dsp_admin_1"]) {
        //官方
        url = [purl stringByAppendingFormat:@"index.php?service=Message.officialLists&p=%d",_paging];
    }else{
        //系统
        url = [purl stringByAppendingFormat:@"index.php?service=Message.systemnotifyLists&uid=%@&p=%d",[Config getOwnID],_paging];
    }
    [YBNetworking postWithUrl:url Dic:nil Suc:^(NSDictionary *data, NSString *code, NSString *msg) {
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
        if ([code isEqual:@"0"]) {
            NSArray *infoA = [data valueForKey:@"info"];
            if (_paging == 1) {
                [_dataArray removeAllObjects];
            }
            if (infoA.count <=0 ) {
                [_tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [_dataArray addObjectsFromArray:infoA];
            }
            if (_dataArray.count <=0) {
                [PublicView showTextNoData:_tableView text1:@"" text2:@"暂无收到消息"];
            }else{
                [PublicView hiddenTextNoData:_tableView];
            }
            [_tableView reloadData];
        }else{
            [YBMsgPop showPop:msg];
        }
    } Fail:^(id fail) {
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
    }];

}
-(void)refreshFooter {
    _paging +=1;
    [self pullData];
}
#pragma mark - UITableViewDelegate、UITableViewDataSource
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 80;
//}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.sysModels.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MsgSysCell *cell = [MsgSysCell cellWithTab:tableView andIndexPath:indexPath];
    cell.model = _sysModels[indexPath.row];
    cell.backgroundColor = CellRow_Cor;
    cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[PublicObj getImgWithColor:SelCell_Col]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MsgSysModel *model = _sysModels[indexPath.row];
    if ([model.uidStr isEqual:@"dsp_admin_1"]) {
        PubH5 *h5 = [[PubH5 alloc]init];
        h5.url = model.urlStr;
        [self.navigationController pushViewController:h5 animated:YES];
    }
    
}

#pragma mark - set/get
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64+statusbarHeight, _window_width, _window_height - 64-statusbarHeight-ShowDiff)style:UITableViewStylePlain];
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = Black_Cor;
        
        //先设置预估行高
        _tableView.estimatedRowHeight = 200;
        //再设置自动计算行高
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
        WeakSelf;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _paging = 1;
            [weakSelf pullData];
        }];
        
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooter)];
        _tableView.mj_footer = footer;
        [footer setTitle:@"数据加载中..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"没有更多了哦~" forState:MJRefreshStateIdle];
        [footer setTitle:@"没有更多了哦~" forState:MJRefreshStateNoMoreData];
        footer.stateLabel.font = [UIFont systemFontOfSize:15.0f];
        footer.automaticallyHidden = YES;
        
    }
    return _tableView;
}

#pragma mark - 导航
-(void)creatNavi {
    YBNavi *navi = [[YBNavi alloc]init];
    navi.leftHidden = NO;
    navi.rightHidden = YES;
    [navi ybNaviLeft:^(id btnBack) {
        [self.navigationController popViewControllerAnimated:YES];
    } andRightName:@"" andRight:^(id btnBack) {
        
    } andMidTitle:_listModel.unameStr];
    [self.view addSubview:navi];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end
